package com.hbx.logs.aspect;

import com.hbx.auth.AuthCacheService;
import com.hbx.constant.OperaResultEnum;
import com.hbx.constant.TableEnum;
import com.hbx.logs.es.EsTemplate;
import com.hbx.logs.pojo.LogSmsInfo;
import com.hbx.logs.annotation.MarkLog;
import com.hbx.model.User;
import com.hbx.task.CompletableFutureService;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

import java.lang.reflect.Method;
import java.util.Date;
import java.util.StringJoiner;

/*
 * @Author:hbx
 * 
 * */
@Aspect
@Component
@Order(2)
public class LogsAspect {

    @Autowired
    private EsTemplate esTemplate;

    @Autowired
    private CompletableFutureService completableFutureService;

    @Pointcut("@annotation(com.hbx.logs.annotation.MarkLog)")
    public void pointcut(){

    }
    @Around("pointcut()")
    public Object aroundAdvice(ProceedingJoinPoint joinPoint) throws Throwable {
        Object[] args = joinPoint.getArgs();
        LogSmsInfo info = new LogSmsInfo();
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        // 获得当前登录用户
        User user = AuthCacheService.getUser();
        MarkLog annotation = method.getAnnotation(MarkLog.class);
        if (user != null){
            info.setOperateor(user.getLoginName());
        }
        if (args != null){
            StringJoiner joiner = new StringJoiner(",");
            for (Object arg : args) {
                joiner.add(arg.toString());
            }
            info.setParams(joiner.toString());
        }
        info.setOperateDesc(annotation.desc());
        info.setOperateClassName(method.getDeclaringClass().getName());
        info.setOperateMethodName(method.getName());
        info.setOperateType(annotation.operate().getOpera());
        TableEnum[] tables = annotation.tables();
        if (!ObjectUtils.isEmpty(tables) && tables.length > 0){
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < tables.length; i++) {
                if (i != tables.length - 1){
                    sb.append(tables[i].getTableName());
                    sb.append(",");
                } else {
                    sb.append(tables[i].getTableName());
                }
            }
            info.setOperateTables(sb.toString());
        }
        Object result = null;
        try {
            result = joinPoint.proceed();
            if (result != null){
                info.setResult(result.toString());
            }
            info.setOperateResult(OperaResultEnum.OPERA_SUCCESS.getDesc());
        } catch (Throwable e) {
            e.printStackTrace();
            info.setOperateWhy(e.getMessage());
            info.setOperateResult(OperaResultEnum.OPERA_ERROR.getDesc());
            throw e;
        } finally {
            info.setOperateDate(new Date());
            // 保存日志
            completableFutureService.runAsyncTask(() -> {
                esTemplate.addLogs(info);
            });
        }
        return result;
    }
}
